package http

import (
	"encoding/json"
	"net"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/sdk/zlog"
)

//
//import (
//	"fmt"
//	"net/http"
//	"qyEnt/common"
//	"qyEnt/common/log"
//
//	"github.com/gin-gonic/gin"
//	ginSwagger "github.com/swaggo/gin-swagger"
//	"github.com/swaggo/gin-swagger/swaggerFiles"
//)
//
//func route(svr *APIServer) {
//	//this.engine.Use(gin.Logger())
//	svr.engine.Use(recovery)
//	//svr.engine.Use(userAuth)
//	svr.engine.Use(func(c *gin.Context) {
//		c.Header("Accept-Language", "zh-CN,zh")
//		c.Header("Accept-Encoding", "gzip;q=1,compress,deflate")
//		c.Header("Access-Control-Allow-Origin", "*")
//		c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,PATCH,HEAD")
//		c.Header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Access-Control-Allow-Origin, Authorization, X-Requested-With")
//		if c.Request.Method == "OPTIONS" {
//			c.Status(http.StatusOK)
//			log.Info("OPTION:", c.Request.RequestURI)
//		} else {
//			c.Next()
//		}
//	})
//	svr.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//	svr.engine.Use(func(c *gin.Context) {
//		span := common.SetSpanForGin(c)
//		defer span.Finish()
//		c.Next()
//	})
//
//	routerInit(svr.engine)
//
//}
//
////统一日志输出
//type httpWriter struct {
//}
//
//// Write 对日志进行封装
//func (writer httpWriter) Write(p []byte) (n int, err error) {
//	if len(p) > 26 {
//		log.Debug(string(p[25:]))
//	} else {
//		log.Debug(string(p))
//	}
//	return len(p), nil
//}
//
//// APIServer 封装的http服务
//type APIServer struct {
//	engine *gin.Engine
//}
//
//// Start 封装的http服务启动
//func (svr *APIServer) Start(ip string, port int) error {
//	//gin.SetMode(gin.ReleaseMode)
//	gin.DefaultWriter = httpWriter{}
//	svr.engine = gin.Default()
//	route(svr)
//	e := svr.engine.Run(fmt.Sprintf("%s:%d", ip, port))
//	return e
//}
//
//// Exit 封装的http服务退出
//func (svr *APIServer) Exit() {
//}

func Process(conn net.Conn) {

	var mm define.Message2
	defer func(conn net.Conn) {
		err := conn.Close()
		if err != nil {

		}
	}(conn)
	buf := make([]byte, 1000)
	n, err := conn.Read(buf[:])
	if err != nil {
		zlog.Error("获取数据出错了", err)
	}
	var s []byte
	for i, v := range buf[:n] {
		if v == 123 {
			s = buf[i:n]
			break
		}
	}
	zlog.Info("收到数据:", string(s))
	err2 := json.Unmarshal(s, &mm)
	if err2 != nil {
		zlog.Error("unmarshal失败", err2)
	}
	zlog.Info(mm)
	_, err = conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n"))
	if err != nil {
		return
	}

	routerInit(&mm)

}
